﻿// ***********************************************************************
// Assembly         : OSS.Doc
// Author           : tianteng
// Created          : 04-28-2020
//
// Last Modified By : tianteng
// Last Modified On : 04-29-2020
// ***********************************************************************
// <copyright file="NewCubeCmd.cs" company="北京智帆高科科技有限公司">
//     Copyright ©  2020
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections.Generic;
using System.Linq;
using Occ.BRepPrimAPI;
using Osg;
using ZFGK.Addins;
using ZFGK.DocFrame.Utility;
using ZFGK.OSG.DocUI;
using ZFGK.OSG.DocUI.CmdEnables;
using ZFGK.OSG.UI;
using ZFGK.OSG.UI.Actions;
using ZFGK.OSG.Utility;
using ZFGK.OsgOcc.Doc.CustomEntity;
using ZFGK.OsgOcc.Utility;
using Shape = Occ.TopoDS.Shape;

namespace OSS.Doc.Cmds
{
    /// <summary>
    /// Class NewCubeCmd.
    /// Implements the <see cref="ZFGK.Addins.IExtendCommand" />
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    [OnlyDocCmdEnable]
    public class NewCubeCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var docViewForm = cmdData.ViewForm as IViewForm;
            if (docViewForm == null) return CmdResult.Cancel;

            var osgViewCtrl = docViewForm.View as ZfOsgDocViewCtrl;
            var actionUtility = osgViewCtrl.ActionUtility;
            var activeDocument = osgViewCtrl.ActiveDocument;

            Vec3d worldPt1, worldPt2;
            if (!actionUtility.PickPoint("选择第一个点", Matrixd.identity(), out worldPt1))
                return CmdResult.Cancel;

            // 临时绘制
            var osgTempDraw = osgViewCtrl.OsgObj.CreateTempDraw();
            if (!actionUtility.PickPoint("选择第二个点", Matrixd.identity(), out worldPt2, delegate (PickPointAction action)
            {
                action.MouseMoveAccept += delegate (Vec3d[] vec3ds)
                {
                    // 绘制一个面
                    var verts = new List<Vec3d>();
                    verts.Add(worldPt1);
                    verts.Add(new Vec3d(worldPt1.x, vec3ds.First().y, 0));
                    verts.Add(vec3ds.First());
                    verts.Add(new Vec3d(vec3ds.First().x, worldPt1.y, 0));

                    var geode = OsgGeometryUtil.CreatePolygon(verts.ToVec3f(), Vec3f.Z_AXIS, new Vec4f(1, 1, 1, 1)).CreateGeode();
                    geode.Name = "1";
                    osgTempDraw.Draw(geode);
                };
            }))
                return CmdResult.Cancel;

            Vec3d worldPt3;
            if (!actionUtility.PickPoint("选择第二个点", new OsgCoordSystem(Vec3d.ZERO, Vec3d.Y_AXIS, Vec3d.Z_AXIS), out worldPt3, delegate (PickPointAction action)
            {
                action.MouseMoveAccept += delegate (Vec3d[] vec3ds)
                {
                    // 绘制一个体
                    var boundingBox = new[] {worldPt1, worldPt2, new Vec3d(worldPt1.x, worldPt1.y, vec3ds.First().z)}.ToVec3f().GetBoundingBox();
                    var geode = OsgGeometryUtil.CreateBox(boundingBox, new Vec4f(1, 1, 1, 1)).CreateGeode();
                    geode.Name = "1";
                    osgTempDraw.Draw(geode);
                };
            }))
                return CmdResult.Cancel;

            osgTempDraw.Dispose();

            // 使用OCC造型
            worldPt1.z = worldPt3.z;
            var makeBox = new MakeBox(worldPt1.ToOccPnt(), worldPt2.ToOccPnt());
            var ent = new ZfOsgOccEntity();
            ent.Shape = new Shape(makeBox.Solid);

            activeDocument.PostToModelSpace(ent, activeDocument.ActiveLayer);

            return CmdResult.Succeed;
        }
    }
}
